package ynu.edu.achitectureclass.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.achitectureclass.entity.Greenhouse;
import ynu.edu.achitectureclass.service.GreenHouseService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/house")
public class GreenHouseController {

    @Autowired
    private GreenHouseService greenHouseService;

    // 获取全部温室数据的 API 接口
    @GetMapping("/all")
    public List<Greenhouse> getAllGreenhouses() {
        return greenHouseService.getAllGreenhouses();
    }

    // 根据ID获取单个温室数据
    @GetMapping("/{id}")
    public ResponseEntity<Greenhouse> getGreenhouseById(@PathVariable Long id) {
        Optional<Greenhouse> greenhouse = greenHouseService.getGreenhouseById(id);
        return greenhouse.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
}